<?php

require '/classes/Connection.class.php';

$conn = new CONNECTION();

$q = mysql_real_escape_string($_GET['q']);

if (is_numeric($q)) {
    $sqlCpf = "SELECT * FROM clients where locate('$q',cpf) > 0 order by locate('$q',cpf) limit 15";

    $resCpf = mysql_query($sqlCpf);

    while ($campo = mysql_fetch_array($resCpf)) {
        $id = $campo['id'];
        $nome = $campo['name'];
        $cpf = $campo['cpf'];
        $cpf = addslashes($cpf);
        $html = preg_replace("/(" . $q . ")/i", "<span style=\"font-weight:bold\">\$1</span>", $nome);
        echo "<li onselect=\"this.setText('$nome').setValue('$cpf','$id');\">$html ($cpf)</li>\n";
    }
} else {
    $sqlName = "SELECT * FROM clients where locate('$q',name) > 0 order by locate('$q',name) limit 50";

    $resName = mysql_query($sqlName);

    while ($campo = mysql_fetch_array($resName)) {
        $id = $campo['id'];
        $nome = $campo['name'];
        $cpf = $campo['cpf'];
        $nome = addslashes($nome);
        $html = preg_replace("/(" . $q . ")/i", "<span style=\"font-weight:bold\">\$1</span>", $nome);
        echo "<li onselect=\"this.setText('$nome').setValue('$cpf','$id');\">$html ($cpf)</li>\n";
    }
}
?>